package com.gdzl.controller;

import com.gdzl.DateEditor;
import com.gdzl.HtmlCleanEditor;
import com.gdzl.JsonMessage;
import com.gdzl.Message;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.swing.*;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

/**
 * Created by hboxs004 on 2018/3/6.
 */
@Controller
public abstract class BaseController {

    protected Logger logger = Logger.getLogger(getClass());

    /**
     * 错误视图
     */
    protected static final String ERROR_VIEW = "/sap/common/error";

    /**
     * 错误消息
     */
    protected static final Message ERROR_MESSAGE = Message.error("asl.message.error");

    /**
     * 成功消息
     */
    protected static final Message SUCCESS_MESSAGE = Message.success("asl.message.success");

    /**
     * "验证结果"参数名称
     */
    private static final String CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME = "constraintViolations";


    @Resource(name = "validator")
    private Validator validator;

    /**
     * 数据绑定
     *
     * @param binder WebDataBinder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new HtmlCleanEditor(true, true));
        binder.registerCustomEditor(Date.class, new DateEditor(true));
    }

    /**
     * 数据验证
     *
     * @param target 验证对象
     * @param groups 验证组
     * @return 验证结果
     */
    protected boolean isValid(Object target, Class<?>... groups) {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(target, groups);
        if (constraintViolations.isEmpty()) {
            return true;
        } else {
            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
            requestAttributes.setAttribute(CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME, constraintViolations, RequestAttributes.SCOPE_REQUEST);
            return false;
        }
    }

    /**
     * 数据验证
     *
     * @param type     类型
     * @param property 属性
     * @param value    值
     * @param groups   验证组
     * @return 验证结果
     */
    protected boolean isValid(Class<?> type, String property, Object value, Class<?>... groups) {
        Set<?> constraintViolations = validator.validateValue(type, property, value, groups);
        if (constraintViolations.isEmpty()) {
            return true;
        } else {
            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
            requestAttributes.setAttribute(CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME, constraintViolations, RequestAttributes.SCOPE_REQUEST);
            return false;
        }
    }


    /**
     * 统一处理异常抛出未知错误
     *
     * @param e 异常
     * @return json
     */
    @ExceptionHandler(RuntimeException.class)
    public
    @ResponseBody
    Object exp(Exception e) {
//        e.printStackTrace();
        logger.warn("Handler execution resulted in exception",e);
        return JsonMessage.UNKNOWN_ERROR;
    }
}
